﻿using PhotoKiosk.Properties;
using PhotoKiosk.ViewModel.Helpers;
using System.Resources;
using System.Windows.Input;

namespace PhotoKiosk.ViewModel.UserControls
{
    public class KeyboardAndTextViewModel : ViewModelBase
    {
        public KeyboardAndTextViewModel()
        {
            loadKeyboard();
        }
        private void loadKeyboard()
        {
            ResourceManager myManager = new ResourceManager(typeof(Resources));
            A1PressCommandText = myManager.GetString("Text_Kb_A1");
            A2PressCommandText = myManager.GetString("Text_Kb_A2");
            A3PressCommandText = myManager.GetString("Text_Kb_A3");
            A4PressCommandText = myManager.GetString("Text_Kb_A4");
            A5PressCommandText = myManager.GetString("Text_Kb_A5");
            A6PressCommandText = myManager.GetString("Text_Kb_A6");
            A7PressCommandText = myManager.GetString("Text_Kb_A7");
            A8PressCommandText = myManager.GetString("Text_Kb_A8");
            A9PressCommandText = myManager.GetString("Text_Kb_A9");
            A10PressCommandText = myManager.GetString("Text_Kb_A10");

            B1PressCommandText = myManager.GetString("Text_Kb_B1");
            B2PressCommandText = myManager.GetString("Text_Kb_B2");
            B3PressCommandText = myManager.GetString("Text_Kb_B3");
            B4PressCommandText = myManager.GetString("Text_Kb_B4");
            B5PressCommandText = myManager.GetString("Text_Kb_B5");
            B6PressCommandText = myManager.GetString("Text_Kb_B6");
            B7PressCommandText = myManager.GetString("Text_Kb_B7");
            B8PressCommandText = myManager.GetString("Text_Kb_B8");
            B9PressCommandText = myManager.GetString("Text_Kb_B9");
            B10PressCommandText = myManager.GetString("Text_Kb_B10");

            C1PressCommandText = myManager.GetString("Text_Kb_C1");
            C2PressCommandText = myManager.GetString("Text_Kb_C2");
            C3PressCommandText = myManager.GetString("Text_Kb_C3");
            C4PressCommandText = myManager.GetString("Text_Kb_C4");
            C5PressCommandText = myManager.GetString("Text_Kb_C5");
            C6PressCommandText = myManager.GetString("Text_Kb_C6");
            C7PressCommandText = myManager.GetString("Text_Kb_C7");
            C8PressCommandText = myManager.GetString("Text_Kb_C8");
            C9PressCommandText = myManager.GetString("Text_Kb_C9");
        }

        public event Handler OnTextChanged;
        public delegate void Handler(string i_text);

        private DelegateCommand m_A1PressCommand;
        private DelegateCommand m_A2PressCommand;
        private DelegateCommand m_A3PressCommand;
        private DelegateCommand m_A4PressCommand;
        private DelegateCommand m_A5PressCommand;
        private DelegateCommand m_A6PressCommand;
        private DelegateCommand m_A7PressCommand;
        private DelegateCommand m_A8PressCommand;
        private DelegateCommand m_A9PressCommand;
        private DelegateCommand m_A10PressCommand;

        private DelegateCommand m_B1PressCommand;
        private DelegateCommand m_B2PressCommand;
        private DelegateCommand m_B3PressCommand;
        private DelegateCommand m_B4PressCommand;
        private DelegateCommand m_B5PressCommand;
        private DelegateCommand m_B6PressCommand;
        private DelegateCommand m_B7PressCommand;
        private DelegateCommand m_B8PressCommand;
        private DelegateCommand m_B9PressCommand;
        private DelegateCommand m_B10PressCommand;

        private DelegateCommand m_C1PressCommand;
        private DelegateCommand m_C2PressCommand;
        private DelegateCommand m_C3PressCommand;
        private DelegateCommand m_C4PressCommand;
        private DelegateCommand m_C5PressCommand;
        private DelegateCommand m_C6PressCommand;
        private DelegateCommand m_C7PressCommand;
        private DelegateCommand m_C8PressCommand;
        private DelegateCommand m_C9PressCommand;

        private DelegateCommand m_spacePressCommand;
        private DelegateCommand m_deletePressCommand;

        private string m_text;
        public string Text
        {
            get
            {
                if (m_text == null)
                {
                    m_text = string.Empty;
                }
                return m_text;
            }
            set
            {
                if (m_text == value)
                {
                    return;
                }
                m_text = value;
                checkInput();
                if (OnTextChanged != null)
                {
                    OnTextChanged(m_text);
                }
                OnPropertyChanged("Text");
            }
        }

        public string A1PressCommandText
        {
            get;
            private set;
        }
        public string A2PressCommandText
        {
            get;
            private set;
        }
        public string A3PressCommandText
        {
            get;
            private set;
        }
        public string A4PressCommandText
        {
            get;
            private set;
        }
        public string A5PressCommandText
        {
            get;
            private set;
        }
        public string A6PressCommandText
        {
            get;
            private set;
        }
        public string A7PressCommandText
        {
            get;
            private set;
        }
        public string A8PressCommandText
        {
            get;
            private set;
        }
        public string A9PressCommandText
        {
            get;
            private set;
        }
        public string A10PressCommandText
        {
            get;
            private set;
        }

        public string B1PressCommandText
        {
            get;
            private set;
        }
        public string B2PressCommandText
        {
            get;
            private set;
        }
        public string B3PressCommandText
        {
            get;
            private set;
        }
        public string B4PressCommandText
        {
            get;
            private set;
        }
        public string B5PressCommandText
        {
            get;
            private set;
        }
        public string B6PressCommandText
        {
            get;
            private set;
        }
        public string B7PressCommandText
        {
            get;
            private set;
        }
        public string B8PressCommandText
        {
            get;
            private set;
        }
        public string B9PressCommandText
        {
            get;
            private set;
        }
        public string B10PressCommandText
        {
            get;
            private set;
        }

        public string C1PressCommandText
        {
            get;
            private set;
        }
        public string C2PressCommandText
        {
            get;
            private set;
        }
        public string C3PressCommandText
        {
            get;
            private set;
        }
        public string C4PressCommandText
        {
            get;
            private set;
        }
        public string C5PressCommandText
        {
            get;
            private set;
        }
        public string C6PressCommandText
        {
            get;
            private set;
        }
        public string C7PressCommandText
        {
            get;
            private set;
        }
        public string C8PressCommandText
        {
            get;
            private set;
        }
        public string C9PressCommandText
        {
            get;
            private set;
        }

        public ICommand A1PressCommand
        {
            get
            {
                if (m_A1PressCommand == null)
                {
                    m_A1PressCommand = new DelegateCommand(param => this.pressCommandExecute(A1PressCommandText));
                }
                return m_A1PressCommand;
            }
        }
        public ICommand A2PressCommand
        {
            get
            {
                if (m_A2PressCommand == null)
                {
                    m_A2PressCommand = new DelegateCommand(param => this.pressCommandExecute(A2PressCommandText));
                }
                return m_A2PressCommand;
            }
        }
        public ICommand A3PressCommand
        {
            get
            {
                if (m_A3PressCommand == null)
                {
                    m_A3PressCommand = new DelegateCommand(param => this.pressCommandExecute(A3PressCommandText));
                }
                return m_A3PressCommand;
            }
        }
        public ICommand A4PressCommand
        {
            get
            {
                if (m_A4PressCommand == null)
                {
                    m_A4PressCommand = new DelegateCommand(param => this.pressCommandExecute(A4PressCommandText));
                }
                return m_A4PressCommand;
            }
        }
        public ICommand A5PressCommand
        {
            get
            {
                if (m_A5PressCommand == null)
                {
                    m_A5PressCommand = new DelegateCommand(param => this.pressCommandExecute(A5PressCommandText));
                }
                return m_A5PressCommand;
            }
        }
        public ICommand A6PressCommand
        {
            get
            {
                if (m_A6PressCommand == null)
                {
                    m_A6PressCommand = new DelegateCommand(param => this.pressCommandExecute(A6PressCommandText));
                }
                return m_A6PressCommand;
            }
        }
        public ICommand A7PressCommand
        {
            get
            {
                if (m_A7PressCommand == null)
                {
                    m_A7PressCommand = new DelegateCommand(param => this.pressCommandExecute(A7PressCommandText));
                }
                return m_A7PressCommand;
            }
        }
        public ICommand A8PressCommand
        {
            get
            {
                if (m_A8PressCommand == null)
                {
                    m_A8PressCommand = new DelegateCommand(param => this.pressCommandExecute(A8PressCommandText));
                }
                return m_A8PressCommand;
            }
        }
        public ICommand A9PressCommand
        {
            get
            {
                if (m_A9PressCommand == null)
                {
                    m_A9PressCommand = new DelegateCommand(param => this.pressCommandExecute(A9PressCommandText));
                }
                return m_A9PressCommand;
            }
        }
        public ICommand A10PressCommand
        {
            get
            {
                if (m_A10PressCommand == null)
                {
                    m_A10PressCommand = new DelegateCommand(param => this.pressCommandExecute(A10PressCommandText));
                }
                return m_A10PressCommand;
            }
        }

        public ICommand B1PressCommand
        {
            get
            {
                if (m_B1PressCommand == null)
                {
                    m_B1PressCommand = new DelegateCommand(param => this.pressCommandExecute(B1PressCommandText));
                }
                return m_B1PressCommand;
            }
        }
        public ICommand B2PressCommand
        {
            get
            {
                if (m_B2PressCommand == null)
                {
                    m_B2PressCommand = new DelegateCommand(param => this.pressCommandExecute(B2PressCommandText));
                }
                return m_B2PressCommand;
            }
        }
        public ICommand B3PressCommand
        {
            get
            {
                if (m_B3PressCommand == null)
                {
                    m_B3PressCommand = new DelegateCommand(param => this.pressCommandExecute(B3PressCommandText));
                }
                return m_B3PressCommand;
            }
        }
        public ICommand B4PressCommand
        {
            get
            {
                if (m_B4PressCommand == null)
                {
                    m_B4PressCommand = new DelegateCommand(param => this.pressCommandExecute(B4PressCommandText));
                }
                return m_B4PressCommand;
            }
        }
        public ICommand B5PressCommand
        {
            get
            {
                if (m_B5PressCommand == null)
                {
                    m_B5PressCommand = new DelegateCommand(param => this.pressCommandExecute(B5PressCommandText));
                }
                return m_B5PressCommand;
            }
        }
        public ICommand B6PressCommand
        {
            get
            {
                if (m_B6PressCommand == null)
                {
                    m_B6PressCommand = new DelegateCommand(param => this.pressCommandExecute(B6PressCommandText));
                }
                return m_B6PressCommand;
            }
        }
        public ICommand B7PressCommand
        {
            get
            {
                if (m_B7PressCommand == null)
                {
                    m_B7PressCommand = new DelegateCommand(param => this.pressCommandExecute(B7PressCommandText));
                }
                return m_B7PressCommand;
            }
        }
        public ICommand B8PressCommand
        {
            get
            {
                if (m_B8PressCommand == null)
                {
                    m_B8PressCommand = new DelegateCommand(param => this.pressCommandExecute(B8PressCommandText));
                }
                return m_B8PressCommand;
            }
        }
        public ICommand B9PressCommand
        {
            get
            {
                if (m_B9PressCommand == null)
                {
                    m_B9PressCommand = new DelegateCommand(param => this.pressCommandExecute(B9PressCommandText));
                }
                return m_B9PressCommand;
            }
        }
        public ICommand B10PressCommand
        {
            get
            {
                if (m_B10PressCommand == null)
                {
                    m_B10PressCommand = new DelegateCommand(param => this.pressCommandExecute(B10PressCommandText));
                }
                return m_B10PressCommand;
            }
        }

        public ICommand C1PressCommand
        {
            get
            {
                if (m_C1PressCommand == null)
                {
                    m_C1PressCommand = new DelegateCommand(param => this.pressCommandExecute(C1PressCommandText));
                }
                return m_C1PressCommand;
            }
        }
        public ICommand C2PressCommand
        {
            get
            {
                if (m_C2PressCommand == null)
                {
                    m_C2PressCommand = new DelegateCommand(param => this.pressCommandExecute(C2PressCommandText));
                }
                return m_C2PressCommand;
            }
        }
        public ICommand C3PressCommand
        {
            get
            {
                if (m_C3PressCommand == null)
                {
                    m_C3PressCommand = new DelegateCommand(param => this.pressCommandExecute(C3PressCommandText));
                }
                return m_C3PressCommand;
            }
        }
        public ICommand C4PressCommand
        {
            get
            {
                if (m_C4PressCommand == null)
                {
                    m_C4PressCommand = new DelegateCommand(param => this.pressCommandExecute(C4PressCommandText));
                }
                return m_C4PressCommand;
            }
        }
        public ICommand C5PressCommand
        {
            get
            {
                if (m_C5PressCommand == null)
                {
                    m_C5PressCommand = new DelegateCommand(param => this.pressCommandExecute(C5PressCommandText));
                }
                return m_C5PressCommand;
            }
        }
        public ICommand C6PressCommand
        {
            get
            {
                if (m_C6PressCommand == null)
                {
                    m_C6PressCommand = new DelegateCommand(param => this.pressCommandExecute(C6PressCommandText));
                }
                return m_C6PressCommand;
            }
        }
        public ICommand C7PressCommand
        {
            get
            {
                if (m_C7PressCommand == null)
                {
                    m_C7PressCommand = new DelegateCommand(param => this.pressCommandExecute(C7PressCommandText));
                }
                return m_C7PressCommand;
            }
        }
        public ICommand C8PressCommand
        {
            get
            {
                if (m_C8PressCommand == null)
                {
                    m_C8PressCommand = new DelegateCommand(param => this.pressCommandExecute(C8PressCommandText));
                }
                return m_C8PressCommand;
            }
        }
        public ICommand C9PressCommand
        {
            get
            {
                if (m_C9PressCommand == null)
                {
                    m_C9PressCommand = new DelegateCommand(param => this.pressCommandExecute(C9PressCommandText));
                }
                return m_C9PressCommand;
            }
        }
        public ICommand SpacePressCommand
        {
            get
            {
                if (m_spacePressCommand == null)
                {
                    m_spacePressCommand = new DelegateCommand(param => this.pressCommandExecute(" "));
                }
                return m_spacePressCommand;
            }
        }
        public ICommand DeletePressCommand
        {
            get
            {
                if (m_deletePressCommand == null)
                {
                    m_deletePressCommand = new DelegateCommand(param => this.deletePressCommandExecute(), param => this.deletePressCommandCanExecute());
                }
                return m_deletePressCommand;
            }
        }

        private bool deletePressCommandCanExecute()
        {
            return !Text.Equals(string.Empty);
        }

        private void deletePressCommandExecute()
        {
            Text = Text.Remove(Text.Length - 1);
        }

        private void pressCommandExecute(string i_input)
        {
            Text += i_input;
        }

        public string m_error = string.Empty;
        public string Error
        {
            get
            {
                return m_error;
            }
            set
            {
                m_error = value;
                OnPropertyChanged("Error");
                OnPropertyChanged("ErrorColor");
            }
        }

        public bool ErrorColor
        {
            get
            {
                return !string.IsNullOrEmpty(Error);
            }
        }

        private void checkInput()
        {
            Error = string.Empty;
            if (string.IsNullOrEmpty(Text))
            {
                ResourceManager myManager = new ResourceManager(typeof(Resources));
                Error = myManager.GetString("Text_Error_Field_Required"); 
            }
        }

        public bool IsValidText()
        {
            checkInput();
            return string.IsNullOrEmpty(Error);
        }
    }
}
